/*
Source

Problem Id:2683  User Id:Latex 
Memory:68K  Time:0MS
Language:G++  Result:Accepted

Source 
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>

	int Ncase;
	int ct;
	int sim[100], com[100];
	int scharge[100], ccharge[100];
	int sc, cc;
	int year;
	int in;
	int sum;
	int best;

int main()
{
	int i, j, k;
	int m;
	double interest;
	int type;
	int intint;
	int rest;
	
	//freopen("p2683.in", "r", stdin);
	//freopen("p2683.out", "w", stdout);
	
	scanf("%ld", &Ncase);
	for (ct=1; ct<=Ncase; ct++)
	{
		//debug
		//printf("now ct = %ld\n", ct );
		
		scanf("%ld", &in);
		scanf("%ld", &year);
		scanf("%ld", &m);
		sc = cc = 0;
		for (i=1; i<=m; i++)
		{
			scanf("%ld%lf", &type, &interest);
			intint = (int) (interest * 8192);
			
			//debug
			//	printf("interest = %lf   intint = %ld\n", interest, intint);
			
			if (type == 0)
			{
				sc++;
				sim[sc] = intint;
				scanf("%ld", scharge+sc);

				//debug
				//	printf("sim[%ld] = %ld\n", sc, sim[sc]);
			} else
			{
				cc++;
				com[cc] = intint;
				scanf("%ld", ccharge+cc);
				
				//debug
				//	printf("com[%ld] = %ld\n", cc, com[cc]);
			}
		}
		
		best = 0;
		// simple
		for (i=1; i<=sc; i++)
		{
			sum = 0;
			rest = in;
			//debug 
			//	printf("rest = %ld\n", rest);
			//	printf("scharge = %ld\n", scharge[i]);
				
			for (j=1; j<=year; j++)
			{
				sum += rest * ((double) sim[i] / 8192);
				rest -= scharge[i];
				//debug
				//	printf("rest = %ld sum = %ld\n", rest, sum);
			}
			sum += rest;
			
			//debug
			//	printf("simple -   %ld\n\n", sum);
				
			if (sum > best)
				best = sum;
		}
		
		// compond
		for (i=1; i<=cc; i++)
		{
			sum = in;
			for (j=1; j<=year; j++)
			{
				
				//debug
				//	printf("sum = %ld\n", sum);
				//	printf("com = %ld\n", com[i]);
				//	printf("interest = %ld\n", (int) (sum * ((long double)com[i] / 8192)));
				
				sum += sum * ((double)com[i] / 8192);
				
				//debug 
				//	printf("sum+interst = %ld\n", sum);
				
				sum -= ccharge[i];
				
				//debug
				//	printf("charge = %ld\n", ccharge[i]);
				//	printf("sum - charge = %ld\n", sum);
			}
			if (sum > best)
				best = sum;

			//debug
			//	printf("compond -   %ld\n\n", sum);
		}
		
		printf("%ld\n", best);
	}
	return 0;
}


